package com.hdsx.jxcsxm.listener;

import com.hdsx.jxcsxm.log.bean.DurationBean;
import com.hdsx.jxcsxm.log.server.LoggingBranchServer;
import com.hdsx.jxcsxm.xtgl.bean.DurationPojo;
import com.hdsx.jxcsxm.xtgl.bean.UserList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import java.util.Date;
import java.util.List;

/**
 * 记录时长
 * @author 占贤银
 * time 2023/8/21 17:04
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Controller
@Scope("prototype")
public class MyDurationListener implements HttpSessionAttributeListener, ServletContextListener {

    private static UserList userList = UserList.getInstance();

    private static WebApplicationContext springContext;

    private LoggingBranchServer server;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        springContext = WebApplicationContextUtils
                .getWebApplicationContext(sce.getServletContext());
        this.server = springContext.getBean(LoggingBranchServer.class);

    }
    @Override
    public void contextDestroyed(ServletContextEvent event) {

    }

    public static void main(String[] args) {
        // 1692629783194
        // 1692629814531
        System.out.println(System.currentTimeMillis());
    }

    /**
     *  * 对Session的添加进行监听！
     *  * name = truename
     *  * value = 九江市公路管理局
     *  * 对Session的添加进行监听！
     *  * name = unit
     *  * value = 21101360400
     *  * @param master
     *
     * @param event
     */
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        final String name = event.getName();
        final String truename = (String) event.getValue();
        if (StringUtils.isNotEmpty(name) && name.equals("truename")) {
            long startTime = System.currentTimeMillis();
            if (userList.isExsist(truename)) {
                final List<DurationBean> durationBeans = server.queryDurationByTruename(truename);
                if (durationBeans != null && durationBeans.size() > 0) {
                    final DurationBean durationBean = durationBeans.get(0);
                    durationBean.setStarttime(String.valueOf(startTime));
                    server.updateDurationByTruename(durationBean);
                } else {
                    DurationBean durationBean = new DurationBean();
                    durationBean.setTruename(truename);
                    durationBean.setStarttime(String.valueOf(startTime));
                    durationBean.setEndtime(String.valueOf(startTime));
                    durationBean.setTotaltime("0");
                    server.insertDurationByTruename(durationBean);
                }
            } else {
                final List<DurationBean> durationBeans = server.queryDurationByTruename(truename);
                if (durationBeans != null && durationBeans.size() > 0) {
                    final DurationBean durationBean1 = durationBeans.get(0);
                    final long totaltime = Long.parseLong(durationBean1.getTotaltime());
                    durationBean1.setTotaltime(String.valueOf(0+totaltime));
                    durationBean1.setStarttime(String.valueOf(System.currentTimeMillis()));
                    server.updateDurationByTruename(durationBean1);
                } else {
                    DurationBean durationBean = new DurationBean();
                    durationBean.setTruename(truename);
                    durationBean.setStarttime(String.valueOf(startTime));
                    durationBean.setEndtime(String.valueOf(startTime));
                    durationBean.setTotaltime("0");
                    server.insertDurationByTruename(durationBean);
                    System.out.println("用户" + truename + "进入了系统...");
                }
            }
            DurationPojo durationPojo = new DurationPojo(truename, startTime);
            userList.addUser(durationPojo);
        }
    }

    /**
     * session已经被移除了
     * name = truename
     * value = 九江市公路管理局
     * session移除时调用这个方法
     * @param event
     */
    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        final String name = event.getName();
        final String truename = (String) event.getValue();

        if (StringUtils.isNotEmpty(name) && name.equals("truename")) {
           /* if (userList.isExsist(truename)) {*/
                final DurationPojo durationBean = userList.getDurationBean(truename);
                final List<DurationBean> durationBeans = server.queryDurationByTruename(truename);
                long startTime = 0L;
                if (durationBean != null) {
                    userList.RemoveUser(durationBean);
                    startTime = durationBean.getStartTime();
                } else {
                    if (durationBeans != null && durationBeans.size() > 0) {
                        startTime = Long.parseLong(durationBeans.get(0).getStarttime());
                    }
                }

                System.out.println("用户数量："+userList.getUserCount());

                // 获取当前时间
                final long nowTime = System.currentTimeMillis();

            long durationMinute = (long)(nowTime-startTime)/(1000*60);

                if (durationBeans != null && durationBeans.size() > 0) {
                    final DurationBean durationBean1 = durationBeans.get(0);
                    final long totaltime = Long.parseLong(durationBean1.getTotaltime());
                    durationBean1.setTotaltime(String.valueOf(durationMinute+totaltime));
                    durationBean1.setEndtime(String.valueOf(nowTime));
                    server.updateDurationByTruename(durationBean1);
                } else {
                    DurationBean bean = new DurationBean();
                    bean.setTruename(truename);
                    bean.setStarttime(String.valueOf(nowTime));
                    bean.setEndtime(String.valueOf(nowTime));
                    bean.setTotaltime(String.valueOf(durationMinute));
                    server.insertDurationByTruename(bean);
                }
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

    }
}
